<?php

namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Survey;
use App\Models\Project;


class SurveyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = function ($query) use ($request) {
            //项目名称
            if ($request->has('project_id') and $request->project_id != '') {

                $query->where('project_id', $request->project_id);
            }
            //负责人
            if ($request->has('leader') and $request->leader != '') {
                $search = "%" . $request->leader . "%";
                $query->where('leader', 'like', $search);
            }
            //区块名称
            if ($request->has('block_name') and $request->block_name != '') {
                $search = "%" . $request->block_name . "%";
                $query->where('block_name', 'like', $search);
            }



        };
        $allProject=Project::all();
        $allSurvey=Survey::with('project')->where($where)->orderBy('created_at', 'desc')->paginate(10);
        return view('admin.survey.index',compact('allSurvey','allProject'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $allProject=Project::all();
        return view('admin.survey.create',compact('allProject'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       return  Survey::newSurvey($request);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /*
     * 编辑调查
     * */
    public function editSurvey(Request $request){
        $allProject=Project::all();
        $arr=explode('=',$request->surveyId);
        $surveyId=$arr['1'];
        $editSurvey=Survey::with('project')->where('id',$surveyId)->first();
        return view('admin.survey.editSurvey',compact('editSurvey','allProject'));
    }

    /*
     * 修改调查
     * */
    public function updateSurvey(Request $request){
        return  Survey::updateSurvey($request);
    }

    /*
    * 删除调查
    * */
    public function deleteSurvey(Request $request){
        $result=Survey::destroy($request->surveyId);
        if($result){
            $backMsg=['msg'=>200];
            return json_encode($backMsg);
        }
    }
}
